#ifndef __DEF_H
#define __DEF_H
#include "stm32f10x.h"                  // Device header
#include "string.h"
#include "stdio.h"
void DEF_Init(void);
//================================================================
//IO port remap
#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2)) 
#define MEM_ADDR(addr)  *((volatile unsigned long  *)(addr)) 
#define BIT_ADDR(addr, bitnum)   MEM_ADDR(BITBAND(addr, bitnum)) 
//IO port addr remap
#define GPIOA_ODR_Addr    (GPIOA_BASE+12) //0x4001080C 
#define GPIOB_ODR_Addr    (GPIOB_BASE+12) //0x40010C0C 
#define GPIOC_ODR_Addr    (GPIOC_BASE+12) //0x4001100C 
#define GPIOD_ODR_Addr    (GPIOD_BASE+12) //0x4001140C 
#define GPIOE_ODR_Addr    (GPIOE_BASE+12) //0x4001180C 
#define GPIOF_ODR_Addr    (GPIOF_BASE+12) //0x40011A0C    
#define GPIOG_ODR_Addr    (GPIOG_BASE+12) //0x40011E0C    

#define GPIOA_IDR_Addr    (GPIOA_BASE+8) //0x40010808 
#define GPIOB_IDR_Addr    (GPIOB_BASE+8) //0x40010C08 
#define GPIOC_IDR_Addr    (GPIOC_BASE+8) //0x40011008 
#define GPIOD_IDR_Addr    (GPIOD_BASE+8) //0x40011408 
#define GPIOE_IDR_Addr    (GPIOE_BASE+8) //0x40011808 
#define GPIOF_IDR_Addr    (GPIOF_BASE+8) //0x40011A08 
#define GPIOG_IDR_Addr    (GPIOG_BASE+8) //0x40011E08 
 
//IO ops,only for single IO port
//ensure n<16
#define PAout(n)   BIT_ADDR(GPIOA_ODR_Addr,n)  //out 
#define PAin(n)    BIT_ADDR(GPIOA_IDR_Addr,n)  //in 

#define PBout(n)   BIT_ADDR(GPIOB_ODR_Addr,n)  //out 
#define PBin(n)    BIT_ADDR(GPIOB_IDR_Addr,n)  //in 

#define PCout(n)   BIT_ADDR(GPIOC_ODR_Addr,n)  //out 
#define PCin(n)    BIT_ADDR(GPIOC_IDR_Addr,n)  //in 

#define PDout(n)   BIT_ADDR(GPIOD_ODR_Addr,n)  //out 
#define PDin(n)    BIT_ADDR(GPIOD_IDR_Addr,n)  //in

#define PEout(n)   BIT_ADDR(GPIOE_ODR_Addr,n)  //out 
#define PEin(n)    BIT_ADDR(GPIOE_IDR_Addr,n)  //in

#define PFout(n)   BIT_ADDR(GPIOF_ODR_Addr,n)  //out 
#define PFin(n)    BIT_ADDR(GPIOF_IDR_Addr,n)  //in

#define PGout(n)   BIT_ADDR(GPIOG_ODR_Addr,n)  //out 
#define PGin(n)    BIT_ADDR(GPIOG_IDR_Addr,n)  //in

//================================================================
//全菊变量
extern const char *devID;
extern u8 devMAC[6];
extern u8 devIP[4];
typedef struct
{
	short	temp;
	short	intemp;
	short	outtemp;
	short	inhum;
	short	outhum;
	short	pm25;
	short	voc;
	short	elec;
	short	volt;
	short	power;
}SENSOR_Obj;
typedef struct
{
	u8	offline;
	u8	tmax;
	u8	tmin;
	u8	senser;
	u8	battery;
}ALARM_Obj;
typedef struct
{
	s8	onoff;			//开关机状态:0表示关机，1表示开机
	s8	temp;				//当前设置温度
	s8	lock;				//[true/false]
	s8	heat;				//[true/false]
	s8	wifi;				//[0~4]
	s8	rf;					//[0~4]
	s8	batter;
	s8	lan;				//
	s8	isai;
	s8	tmin;				//低温保护温度:防冻保护温度，低于5度表示关闭此功能
	s8	tmax;
	s8	tc;
	s8	tsmax;
	s8	tsmin;
	s8	smode;
	s8	stype;
	s8	alevel;
	s8	bltime;
	s8	bllevel;
	s8	led;
}EQUIP_Obj;
typedef struct
{
	int	year;
	int	month;
	int	day;
	int	hour;
	int	min;
	int	sec;
}DATE_Obj;
typedef struct
{
	u8	ap_link;
	u8	ip_link;
	u8	tcp_link;
	
	u8	get_mac;
	u8	start_xmit; //透传模式
	u8	mqtt_conn;	//mqtt服务器连接
	u8	mqtt_sync;	//时间是否同步
	u8	mqtt_reg_dev;//设备是否注册
}CONN_Obj;
typedef struct
{
	CONN_Obj		conn;
	DATE_Obj		date;
	EQUIP_Obj		equip;
	ALARM_Obj		alarm;
	SENSOR_Obj	sensor;
}SYS_Obj;
extern SYS_Obj gSys;
//USART3
#define USART3_RX_MAX 512
extern u8 USART3_DMA_RXBuf[USART3_RX_MAX];
extern u16	USART3_Recv_Len;
//ESP
#if 0
#define	ESP_SendString(msg,len) USART3_DMA_SendString(msg,len)
#define ESP_Recv_Len USART3_Recv_Len
#else
#define	ESP_SendString(msg,len) U3_SendString(msg,len)
#define ESP_Recv_Len USART3_Recv_Len
#endif
//MQTT
//MQTT===========================================================================
#define		MQTT_CMD_LEN	512//消息buf长度

#define		MQTT_TypeCONNECT							1//请求连接  
#define		MQTT_TypeCONNACK							2//请求应答  
#define		MQTT_TypePUBLISH							3//发布消息  
#define		MQTT_TypePUBACK								4//发布应答  QoS1
#define		MQTT_TypePUBREC								5//发布已接收，保证传递1  
#define		MQTT_TypePUBREL								6//发布释放，保证传递2  
#define		MQTT_TypePUBCOMP							7//发布完成，保证传递3  
#define		MQTT_TypeSUBSCRIBE						8//订阅请求  
#define		MQTT_TypeSUBACK								9//订阅应答  
#define		MQTT_TypeUNSUBSCRIBE					10//取消订阅  
#define		MQTT_TypeUNSUBACK							11//取消订阅应答  
#define		MQTT_TypePINGREQ							12//ping请求  
#define		MQTT_TypePINGRESP							13//ping响应  
#define		MQTT_TypeDISCONNECT 					14//断开连接 

#define		TOPIC_SYNC				"$SYS/broker/time"
#define		TOPIC_DEV_REG			"w2c/iot/register/"	//+devID
#define		TOPIC_DEV_REG_ACK	"w2c/iot/register/reply/" //+devID
#define		TOPIC_RUNTIME			"w2c/iot/runtime/"	//+devID
#define		TOPIC_ALARM				"w2c/iot/alarm/"		//+devID
#define		TOPIC_SENSOR			"w2c/iot/sensor/"		//+devID
#define		TOPIC_BEHAVIOR		"w2c/iot/behavior/"	//+devID
#define		TOPIC_HISTORY			"w2c/iot/history/"	//+devID
#endif
